
clear all
************************************
*Numeric Export disabled
local TEX ""
local TEXON 0 //Change "0" to "1" to enable exporting numeric results to .txt files
if !`TEXON' local TEX "*"
*Also need to install `texresults2
*ssc install texresults2
************************************

************************************************
*CEM Simulation: PARTISANSHIP
*Appendix Table A-4, Column 3
************************************************

*Balance on Republican preferences

**********************
*Set working directory
**********************

use "Enns_SociologicalScience_Reproduction\DS1_0.dta", clear

*****************
**Recode outcome so dichotomous
*****************
gen policyadopted =.
recode policyadopted .=0 if OUTCOME==0
recode policyadopted .=1 if OUTCOME==2
recode policyadopted .=1 if OUTCOME==3
recode policyadopted .=1 if OUTCOME==4

************************************************************
**Calculate Percent Favor Among Dems, Reps, & Ind
gen pid_cat = 3
recode pid_cat 3=4 if pid4_fav!=0 & pid5_fav==0 & pid6_fav==0 & pid7_fav==0
recode pid_cat 3=4 if pid4_fav!=. & pid4_fav!=0 & pid5_fav==. & pid6_fav==. & pid7_fav==. 
recode pid_cat 3=5 if pid4_fav!=0 & pid5_fav!=0 & pid6_fav==0 & pid7_fav==0
recode pid_cat 3=5 if pid4_fav!=. & pid5_fav!=. & pid4_fav!=0 & pid5_fav!=0 & pid6_fav==. & pid7_fav==.
recode pid_cat 3=6 if pid4_fav!=0 & pid5_fav!=0 & pid6_fav!=0 & pid7_fav==0
recode pid_cat 3=6 if pid4_fav!=. & pid5_fav!=. & pid6_fav!=. & pid4_fav!=0 & pid5_fav!=0 & pid6_fav!=0 & pid7_fav==.
recode pid_cat 3=7 if pid7_fav!=. & pid7_fav!=0

gen perfav_rep = .
replace perfav_rep = pid1_fav/(pid1_fav+pid1_opp+pid1_dk) if pid_cat==3
*recode so all responses in direction of policy change (following Gilens)
replace perfav_rep = pid1_opp/(pid1_fav+pid1_opp+pid1_dk) if pid_cat==3 & switcher==1

replace perfav_rep = (pid1_fav+pid2_fav)/(pid1_fav+pid1_opp+pid1_dk+pid2_fav+pid2_opp+pid2_dk) if pid_cat==5
*recode so all responses in direction of policy change (following Gilens)
replace perfav_rep = (pid1_opp+pid2_opp)/(pid1_fav+pid1_opp+pid1_dk+pid2_fav+pid2_opp+pid2_dk) if pid_cat==5 & switcher==1

replace perfav_rep = (pid1_fav+pid2_fav+pid3_fav)/(pid1_fav+pid1_opp+pid1_dk+pid2_fav+pid2_opp+pid2_dk+pid3_fav+pid3_opp+pid3_dk) if pid_cat==7
*recode so all responses in direction of policy change (following Gilens)
replace perfav_rep = (pid1_opp+pid2_opp+pid3_opp)/(pid1_fav+pid1_opp+pid1_dk+pid2_fav+pid2_opp+pid2_dk+pid3_fav+pid3_opp+pid3_dk) if pid_cat==7 & switcher==1

gen perfav_rep_lor = log(perfav_rep/(1- perfav_rep))

gen perfav_dem = .
replace perfav_dem = pid3_fav/(pid3_fav+pid3_opp+pid3_dk) if pid_cat==3
*recode so all responses in direction of policy change (following Gilens)
replace perfav_dem = pid3_opp/(pid3_fav+pid3_opp+pid3_dk) if pid_cat==3 & switcher==1

replace perfav_dem = (pid5_fav+pid4_fav)/(pid5_fav+pid5_opp+pid5_dk+pid4_fav+pid4_opp+pid4_dk) if pid_cat==5
*recode so all responses in direction of policy change (following Gilens)
replace perfav_dem = (pid5_opp+pid4_opp)/(pid5_fav+pid5_opp+pid5_dk+pid4_fav+pid4_opp+pid4_dk) if pid_cat==5 & switcher==1

replace perfav_dem = (pid7_fav+pid6_fav+pid5_fav)/(pid7_fav+pid7_opp+pid7_dk+pid6_fav+pid6_opp+pid6_dk+pid5_fav+pid5_opp+pid5_dk) if pid_cat==7
*recode so all responses in direction of policy change (following Gilens)
replace perfav_dem = (pid7_opp+pid6_opp+pid5_opp)/(pid7_fav+pid7_opp+pid7_dk+pid6_fav+pid6_opp+pid6_dk+pid5_fav+pid5_opp+pid5_dk) if pid_cat==7 & switcher==1

gen perfav_dem_lor = log(perfav_dem/(1- perfav_dem))

gen perfav_ind = .
replace perfav_ind = pid2_fav/(pid2_fav+pid2_opp+pid2_dk) if pid_cat==3
*recode so all responses in direction of policy change (following Gilens)
replace perfav_ind = pid2_opp/(pid2_fav+pid2_opp+pid2_dk) if pid_cat==3 & switcher==1

replace perfav_ind = (pid3_fav)/(pid3_fav+pid3_opp+pid3_dk) if pid_cat==5
*recode so all responses in direction of policy change (following Gilens)
replace perfav_ind = (pid3_opp)/(pid3_fav+pid3_opp+pid3_dk) if pid_cat==5 & switcher==1

replace perfav_ind = (pid4_fav)/(pid4_fav+pid4_opp+pid4_dk) if pid_cat==7
*recode so all responses in direction of policy change (following Gilens)
replace perfav_ind = (pid4_opp)/(pid4_fav+pid4_opp+pid4_dk) if pid_cat==7 & switcher==1

gen perfav_ind_lor = log(perfav_ind/(1- perfav_ind))

********************************************

*generate dichotomous variables to capture group intercent
gen rep_ind = .
recode rep_ind .=1 if (perfav_rep - perfav_ind)>.1 & perfav_rep!=. & perfav_ind!=.
recode rep_ind .=0 if (perfav_ind-perfav_rep)>.1 & perfav_rep!=. & perfav_ind!=.

gen rep_dem = .
recode rep_dem .=1 if (perfav_rep - perfav_dem)>.1 & perfav_rep!=. & perfav_dem!=.
recode rep_dem .=0 if (perfav_dem - perfav_rep)>.1 & perfav_rep!=. & perfav_dem!=.
****************************************************

*******************************************************************
**save data as temporary file to be used below 
tempfile DS1_pid
save "`DS1_pid'", replace
*******************************************************************

program define cem_pid_rep
args DS1_pid
use "`DS1_pid'", clear
*Republicans (vs Independents)
*drop missing differences (less than abs value(10%))
quietly: drop if rep_ind==.
quietly: cem perfav_rep if policyadopted!=., tr(rep_ind) k2k
quietly:logit policyadopted perfav_rep_lor if cem_matched==1

mat def coef = e(b)
scalar repVind = coef[1,1]
scalar repVind_cons = coef[1,2]
scalar N_repVind = e(N)
end

********************************************************************************
*Balance on Independent preferences
********************************************************************************
program define cem_pid_ind
args DS1_pid
use "`DS1_pid'", clear
*Republicans (vs Independents)
*drop missing differences (less than abs value(10%))
quietly: drop if rep_ind==.
quietly: cem perfav_ind if policyadopted!=., tr(rep_ind) k2k
quietly:logit policyadopted perfav_ind_lor if cem_matched==1

mat def coef = e(b)
scalar indVrep = coef[1,1]
scalar indVrep_cons = coef[1,2]
scalar N_indVrep = e(N)

end

********************************************************************************
*Balance on Democrat preferences
********************************************************************************
program cem_pid_dem
args DS1_pid
use "`DS1_pid'", clear
*Democrats (vs Republicans)
*drop missing differences (less than abs value(10%))
quietly: drop if rep_dem==.
quietly: cem perfav_dem if policyadopted!=., tr(rep_dem) k2k
quietly:logit policyadopted perfav_dem_lor if cem_matched==1

mat def coef = e(b)
scalar demVrep = coef[1,1]
scalar demVrep_cons = coef[1,2]
scalar N_demVrep = e(N)
end

******************************************
*Run above programs
******************************************

//set up a new dataset with variable names listed below called cem_data
tempfile cem_pid_data
tempname temp1
postfile `temp1' repVind repVind_cons N_repVind ///
				indVrep indVrep_cons N_indVrep ///
				demVrep demVrep_cons N_demVrep  ///
				using "`cem_pid_data'", replace

set seed 6951929
//run each program 1000 times 
forval i = 1/1000 {
	
cem_pid_rep "`DS1_pid'"
cem_pid_ind "`DS1_pid'"
cem_pid_dem "`DS1_pid'"

//post results from each simulation to data set 
post `temp1' (repVind) (repVind_cons) (N_repVind) (indVrep) (indVrep_cons) (N_indVrep) ///
			(demVrep) (demVrep_cons) (N_demVrep) 

}

postclose `temp1' //write results to dataset 

******************************************************************************
*export results 
use "`cem_pid_data'", clear 
sum 

local varlist repVind repVind_cons indVrep indVrep_cons demVrep demVrep_cons

foreach var of local varlist { 
	//generate locals of median and 95% bounds

	_pctile `var', nq(1000)
	local m_`var' = r(r500)
di `m_`var''
	local lb_`var' = r(r25) 
di `lb_`var''
    local ub_`var' = r(r975) 	
di `ub`var''
}
sum N_repVind
local nrep = r(mean)
sum N_indVrep
local nind = r(mean)
sum N_demVrep
local ndem = r(mean)
//Republican (vs Independent)
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(medrep) result(`m_repVind') replace
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(lbrep) result(`lb_repVind') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(ubrep) result(`ub_repVind') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(medrepcons) result(`m_repVind_cons') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(lbrepcons) result(`lb_repVind_cons') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(ubrepcons) result(`ub_repVind_cons') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(nrep) result(`nrep') round(0) append


//Independent (vs Republican)
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(medind) result(`m_indVrep') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(lbind) result(`lb_indVrep') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(ubind) result(`ub_indVrep') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(medindcons) result(`m_indVrep_cons') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(lbindcons) result(`lb_indVrep_cons') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(ubindcons) result(`ub_indVrep_cons') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(nind) result(`nind') round(0) append

//Democrat (vs Republican)
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(meddem) result(`m_demVrep') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(lbdem) result(`lb_demVrep') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(ubdem) result(`ub_demVrep') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(meddemcons) result(`m_demVrep_cons') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(lbdemcons) result(`lb_demVrep_cons') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(ubdemcons) result(`ub_demVrep_cons') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem_pid.txt, texmacro(ndem) result(`ndem') round(0) append

************************************************
************************************************
************************************************





